package com.unionServer.common;

import java.io.UnsupportedEncodingException;

/**
 * @description:
 * @author: wangxinal
 * @time: 2022/1/19 14:05
 */
public class LBCDHexConverter {
    public LBCDHexConverter() {
    }
    public final String bytesToHexString(byte[] bArray) {
        StringBuffer sb = new StringBuffer(bArray.length);

        for(int i = 0; i < bArray.length; ++i) {
            String sTemp = Integer.toHexString(255 & bArray[i]);
            if (sTemp.length() < 2) {
                sb.append(0);
            }

            sb.append(sTemp.toUpperCase());
        }

        return sb.toString();
    }

    public byte[] getFromBcd(byte[] arg0, int definedLen, String encoding) {
        String s = bytesToHexString(arg0);
        if (definedLen > 0) {
            if (definedLen > s.length()) {
                while(s.length() < definedLen) {
                    s = s + "0";
                }
            } else if (definedLen < s.length()) {
                s = s.substring(0, definedLen);
            }
        }

        try {
            return s.getBytes(encoding);
        } catch (UnsupportedEncodingException var7) {
            var7.printStackTrace();
            return s.getBytes();
        }
    }
}
